﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.UI;
using System.Web;

namespace Churchtoolkit.Utils.Navigation
{
    public static class Navigator
    {
        #region [ helper ]
        private static string ResolveUrl(Pages page)
        {
            var enumMember = page.GetType().GetMember(page.ToString())[0];
            var attribute =
                Attribute.GetCustomAttribute(enumMember, typeof (DescriptionAttribute)) as DescriptionAttribute;
            return attribute != null ? attribute.Description : string.Empty;
        }
        private static string FormatUrl(string url,params string[] arguments)
        {
            if(arguments == null || arguments.Length == 0)
                return url;
            return string.Format("{0}?{1}", url, string.Join("&", arguments));
        }
        private static void Redirect(string url)
        {
            HttpContext.Current.Response.Redirect(url);
        }
        #endregion

        /// <summary>
        /// Navigates to.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="arguments">The arguments.</param>
        public static void NavigateTo(Pages page,params string[] arguments)
        {
            Redirect(FormatUrl(ResolveUrl(page), arguments));
        }
        /// <summary>
        /// Resolves the URL.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="arguments">The arguments.</param>
        /// <returns></returns>
        public static string ResolveUrl(Pages page,params string[] arguments)
        {
            return FormatUrl(ResolveUrl(page), arguments);
        }
    }
}
